package com.sctvcloud.linshui.ui.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.ruihang.generalibrary.utils.DateUtils;
import com.sctvcloud.linshui.R;
import com.sctvcloud.linshui.base.BaseActivity;
import com.sctvcloud.linshui.beans.FProgram;
import com.sctvcloud.linshui.beans.LookBackBean;
import com.sctvcloud.linshui.ui.adapter.holder.LookBackNewsHolder;
import com.sctvcloud.linshui.ui.utils.GlideUtil;

import org.xutils.common.util.LogUtil;

import java.util.List;

public class LookBackNewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private LookBackNewsHolder.OnColumnClickListener listener;
    private LookBackNewsHolder.OnColumnItemClickListener itemClickListener;
    private List<LookBackBean> dataList;
    private BaseActivity activity;
    private Handler handler = new Handler();
    private boolean isMute = true;

    public LookBackNewsAdapter(Context context, List<LookBackBean> list, LookBackNewsHolder.OnColumnClickListener listener, LookBackNewsHolder.OnColumnItemClickListener itemClickListener) {
        this.context = context;
        this.listener = listener;
        this.itemClickListener = itemClickListener;
    }

//    @Override
//    public BaseAbsHolder<LookBackBean> onCreateViewHolder(ViewGroup parent, int viewType) {
//        BaseAbsHolder bah = null;
//        View v = LayoutInflater.from(context).inflate(R.layout.news_lookback_item, parent, false);
//        bah = new LookBackNewsHolder(context, v, listener, itemClickListener);
//        return bah;
//    }
//
//    @Override
//    public void onBindViewHolder(BaseAbsHolder<LookBackBean> holder, int position) {
//        super.onBindViewHolder(holder, position);
//    }

    public BaseActivity getActivity() {
        return activity;
    }

    public void setActivity(BaseActivity activity) {
        this.activity = activity;
    }

    @Override
    public int getItemCount() {
        if (null != dataList) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View v = LayoutInflater.from(context).inflate(R.layout.news_lookback_item, parent, false);
        holder = new LookBackNewsHolder(v);
        return holder;
    }

    public void setData(List<LookBackBean> list) {
        if (null != list) {
            dataList = list;
        }
        notifyDataSetChanged();
    }

    public void addData(List<LookBackBean> list) {
        int pos = getItemCount();
        if (null != list) {
            dataList.addAll(list);
        }
        if (pos > 0) {
            notifyItemRangeChanged(pos, getItemCount());
        }
    }

    public void updateData(List<LookBackBean> list) {
        if (null != list) {
            dataList = list;
        }
    }

    public boolean isMute() {
        return isMute;
    }

    public void setMute(boolean mute) {
        isMute = mute;
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getDataViewType();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final LookBackBean program = dataList.get(position);
        final LookBackNewsHolder viewHolder = (LookBackNewsHolder) holder;
        viewHolder.columnName.setText(program.getProgrammeName());
        if (null != program.getProgram()) {
            final FProgram fProgram = program.getProgram();
            viewHolder.programTitle.setText(fProgram.getProgrammeTitle());
            if (!TextUtils.isEmpty(fProgram.getPubTime())) {
                viewHolder.programDate.setText(DateUtils.formatDate(DateUtils.getDataDate(fProgram), ""));
            } else {
                viewHolder.programDate.setText("");
            }
//            viewHolder.programDate.setText(DateUtils.formatDate(DateUtils.getDataDate(fProgram), ""));
            viewHolder.setfProgram(fProgram);
            if (null != itemClickListener && !TextUtils.isEmpty(fProgram.getProgrammeUrl())) {
//                viewHolder.videoPlayer.
                itemClickListener.OnItemInit(viewHolder.videoPlayer, position);
            }
            viewHolder.playerBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != itemClickListener) {
                        itemClickListener.OnItemClick(fProgram, position);
                    }
                }
            });
            viewHolder.videoPlayer.setMuteChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    isMute = isChecked;
                    LogUtil.e("setMuteChangeListener isChecked = " + isChecked);
                }
            });

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (viewHolder.videoPlayer.getThumbImge() != null && null != activity && !activity.isDestroyed()) {
                        GlideUtil.getGlideWithLarge169DefNotFit(context, fProgram.getProgrammeImage()).into(viewHolder.videoPlayer.getThumbImge());
                    }
                    viewHolder.videoPlayer.setMute(isMute);
                    viewHolder.videoPlayer.setUp(fProgram.getProgrammeUrl(), false, "");
                    viewHolder.videoPlayer.onVideoPause();

                }
            }, 200);
        }
        viewHolder.columnLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onColumnClick(program);
                }
            }
        });
        viewHolder.bottomTitleLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onColumnClick(program);
                }
            }
        });
    }
}
